'use strict';

import { userwinhistoryModel } from "../model/userwinhistory";
import { ServiceBase, Exceptions } from '../kalend';
import { platform_userService } from "./platform_user";
import { invitationawardService } from "./invitationaward";
var moment = require('moment');

class UserwinhistoryService extends ServiceBase {
    constructor() {
        super(userwinhistoryModel)
    }
    async query() {
        return super.queryAll();
    }
    async getlevel(puserid: any) {
        let time = 2;
        let fmoney = 10;
        let smoney = 5;
        let tmoney = 2;
        let addmoney = 0;
        let firstcount = 3000;
        let secondcount = 6000;
        let threecount;

        const puser = await platform_userService.queryById(puserid);
        if (puser.citytype) {
            switch (puser.citytype) {
                case 1:
                    firstcount = 6000;
                    secondcount = 12000;
                    break;
                case 2:
                    firstcount = 4500;
                    secondcount = 9000;
                    break;
                case 3:
                    firstcount = 3000;
                    secondcount = 6000;
                    break;
                default:
                    break;
            }
        }
        // 查询是否有当前城市奖励配置
        let ward = await invitationawardService.getsetward(puser);
        if (ward) {
            if (ward.time) {
                time = ward.time;
            }
            if (ward.fnum) {
                firstcount = ward.fnum;
            }
            if (ward.fmoney) {
                fmoney = ward.fmoney;
            }
            if (ward.snum) {
                secondcount = ward.snum;
            }
            if (ward.smoney) {
                smoney = ward.smoney;
            }
            if (ward.tnum) {
                threecount = ward.tnum;
            }
            if (ward.tmoney) {
                tmoney = ward.tmoney;
            }
        }
        console.log('查询是否有当前城市奖励配置:' + ward ? 'true' : false);


        const pcreatetime = puser.createTime;
        const count = await this.count({ platform_user: puserid });

        const endtime = moment(pcreatetime).add(time, 'month').format('YYYY-MM-DD');
        //moment('2010-10-20').isBefore('2010-10-21');
        const today = moment(new Date()).format('YYYY-MM-DD');
        const timeok = moment(today).isBefore(endtime);
        if (timeok) {
            if (count <= firstcount) {
                addmoney = fmoney;
            } else if (count > firstcount && count <= secondcount) {
                addmoney = smoney;
            } else {
                if (threecount) {
                    if (count <= threecount) {
                        addmoney = tmoney;
                    } else {
                        addmoney = 0;
                    }
                } else {
                    addmoney = tmoney;
                }
            }
        } else {
            addmoney = 2;
        }

        console.log('查询奖励金额为:' + addmoney);
        return addmoney;
    }

    async getuserwinhis(params: any) {
        const offset = params.offset || 1;
        const limit = params.limit || 15;
        const uid = params.uid;
        const starttime = params.starttime;
        const endtime = params.endtime;
        let filter: any = {};
        if (uid) {
            filter.platform_user = uid;
        }
        if (starttime) {
            filter.createTime = { '$gte': new Date(moment(new Date(starttime)).format('YYYY-MM-DD') + '  00:00:00') };
        }
        if (endtime) {
            if (filter.createTime) {
                filter.createTime['$lte'] = new Date(moment(endtime).format('YYYY-MM-DD') + '  23:59:59');
            } else {
                filter.createTime = { '$lte': new Date(moment(endtime).format('YYYY-MM-DD') + '  23:59:59') };
            }
        }
        let data: any = await this.queryPagination(offset, limit, {
            filter: filter,
            populateOptions: [
                {
                    path: 'merchantid',
                    select: 'name'
                },
                {
                    path: 'customerid',
                    select: 'nickname phone'
                },
                {
                    path: 'inviterid',
                    select: 'nickname phone'
                }
            ],
            select: 'merchantid customerid inviterid money balance createTime'
        });
        data.rows = data.rows.map((item: any) => {
            if (item.merchantid) {
                item.mername = item.merchantid.name;
            }
            if (item.inviterid) {
                item.invitername = item.inviterid.nickname;
                item.inviterphone = item.inviterid.phone;
            } else {
                item.inviter = '会员邀请';
            }
            if (item.customerid) {
                item.cusname = item.customerid.nickname;
                item.cusphone = item.customerid.phone;
            }
            delete item.merchantid;
            delete item.customerid;
            delete item.inviterid
            return item;
        });
        if (uid) {
            const puser = await platform_userService.queryById(uid);
            let znum = await this.count({inviterid: puser.customerid, platform_user:uid});
            data.footTotal = `招募会员共计${data.total}位，其中直招${znum}位，间接招募${data.total - znum}位`;
        }
        return data;
    }
}

export const userwinhistoryService = new UserwinhistoryService();